Lesson 6 - Identify loops

It is very common for most of a programs execution time to reside in a loop. On average 80% of the time is spent in only 20% of the code. As such it is important to identify the loops needed in a problem. When looking for loops you are looking for anything which is repetitive.

"Given a set of test scores you are to work out the average test score, the maximum test score and finally the lowest test code. "

On the surface there is nothing here to repeat. However working out an average does require a loop! In order to work out an average you must add up all the values of a list. So each elelement of the list must be added to the total.

for each element in the list
add the current value to the total
repeat until all values have been looked at


def testScoreEval(testScores):
	min = testScores[0]
	max = testScores[0]
	total = 0
	for i in testScores:
		total = total + i
		if min < i: min = i
		if max > i: max = i
	
	average = total / len(testScores)
	print "max = ", max
	print "min = ", min
	print "average = ", average